#version 330 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D inputTexture;
uniform vec2 textureSize;
uniform int direction;
uniform float weights[512];
uniform int radius;

void main() {
    vec2 texOffset = 1.0 / textureSize;
    vec4 result = texture(inputTexture, TexCoords);
    result.rgb *= result.a;
    result *= weights[radius];

    if (direction == 0) {
        for (int i = 1; i <= radius; ++i) {
            vec4 addition0 = texture(inputTexture, TexCoords + vec2(texOffset.x * i, 0.0));
            addition0.rgb *= addition0.a;
            addition0 *= weights[radius + i];
            vec4 addition1 = texture(inputTexture, TexCoords - vec2(texOffset.x * i, 0.0));
            addition1.rgb *= addition1.a;
            addition1 *= weights[radius + i];
            result += addition0 + addition1;
        }
    }
    else {
        for (int i = 1; i <= radius; ++i) {
            vec4 addition0 = texture(inputTexture, TexCoords + vec2(0.0, texOffset.y * i));
            addition0.rgb *= addition0.a;
            addition0 *= weights[radius + i];
            vec4 addition1 = texture(inputTexture, TexCoords - vec2(0.0, texOffset.y * i));
            addition1.rgb *= addition1.a;
            addition1 *= weights[radius + i];
            result += addition0 + addition1;
        }
    }
    if (result.a == 0) {
        FragColor = vec4(0, 0, 0, 0);
        return;
    }
    result.rgb /= result.a;
    FragColor = result;
}